#######################
#DATA ON PARTY SYSTEMS#
#######################

#MARIANA CARMO DUARTE

#AUGUST 2024

#Libraries
library(haven)
library(foreign)
library(scales)
library(dplyr)

setwd("")
partsys <- read.csv2("PARLGOV.csv")


##############
#####CHES#####
##############

#LOAD DATA
setwd("")
partCHES <- read.spss("PoliticalParties_CHES_ELECTIONS.sav", use.value.labels = TRUE, to.data.frame = TRUE)
var_control <- read.spss("variables_final.sav", use.value.labels = TRUE, to.data.frame = TRUE)

####ELECTION ID###
#COUNTRY NAMES
new_dta_ches <- subset(partCHES, select =c(election_id, country_electionyear, country, electionyear, year))
duplicated(new_dta_ches)
party_data <- new_dta_ches[!duplicated(new_dta_ches$election_id),]

party_system_ches <- merge(partsys, party_data, by="election_id")
party_system_ches <- merge(party_system_ches, var_control, by=c("country","electionyear"))

#ADD POLITICISATION INDEX
poli <- subset(partCHES, select =c(election_id, countryear, politicisation, eu_polarisation_ps, eu_salience_ps, Dalton, salience.Dalton))
duplicated(poli)
poli <- subset(poli, countryear != "be  2002")
poli <- subset(poli, countryear != "fr  2019")
pd <- poli[!duplicated(poli$election_id),]
pd <- subset(pd, select =c(election_id, politicisation, eu_polarisation_ps, eu_salience_ps, Dalton, salience.Dalton))

poli <- subset(partCHES, select =c(election_id, politicisation, eu_polarisation_ps, eu_salience_ps, Dalton, salience.Dalton))

PS_CHES <-merge(party_system_ches, pd, by="election_id")

################
#####MARPOR#####
################

#LOAD DATA
setwd("")
partMARPOR <- read.spss("PoliticalParties_MARPOR_ELECTIONS.sav", use.value.labels = TRUE, to.data.frame = TRUE)

#COUNTRY NAMES 
new_dta_marpor <- subset(partMARPOR, select =c(election_id, country_electionyear, country, electionyear))
duplicated(new_dta_marpor)
party_data <- new_dta_marpor[!duplicated(new_dta_marpor$election_id),]

party_system_marpor <- merge(partsys, party_data, by="election_id")

#ADD POLITICISATION INDEX
poli <- subset(partMARPOR, select =c(election_id, politicisation, eu_polarisation_ps, eu_salience_ps, politicisation2, eu_polarisation_ps2, eu_salience_ps2, Dalton, salience.Dalton))
duplicated(poli)
pd <- poli[!duplicated(poli$election_id),]

PS_MARPOR <-merge(party_system_marpor, pd, by="election_id")

###################
###BOTH DATASETS###
###################

PS_CHES_MARPOR <- merge(PS_CHES, PS_MARPOR, by="election_id", all=TRUE)
setwd("")
write_sav(PS_CHES_MARPOR, "PartySystems_CHES_MARPOR_PARLGOV.sav")
write.csv2(PS_CHES_MARPOR, "PartySystems_CHES_MARPOR_PARLGOV.csv")

